package codeHuffman2;

import java.io.BufferedInputStream;    
import java.io.IOException;   
   
public class BitReader {   
       
    protected BufferedInputStream file;   
    protected int readCount;   
    protected byte byteCache;   
    public BitReader(BufferedInputStream file){   
        this.file = file;   
        readCount = 8;   
    }   
    
    //lire un bit du fichier
    public byte readBit(){   
        if(readCount == 8){   
            int value = -1;   
            try {   
                value = file.read();   
            } catch (IOException e) {      
                e.printStackTrace();   
            }   
            if(value == -1){   
                return -1;   
            }   
            byteCache = (byte)value;    
            readCount = 0;   
        }   
        int masque =  0x80 >>> readCount;   
        readCount ++;   
        return (byte)((byteCache & masque) >>> ( 8 - readCount));              
    }   
}  